package main

import (
	"github.com/cz-it/aps/src/config"
	"github.com/cz-it/aps/src/log"

	"github.com/jessevdk/go-flags"

	"os"
	"fmt"
)

var opts struct {
	Daemon bool   `short:"d" long:"daemon" description:"Weather start as daemon"`
	Config string `short:"c" long:"config" description:"Config file to use" required:"yes"`
}

func main() {
	var err error
	// parse flags
	parse := flags.NewParser(&opts, flags.Default)
	_, err = parse.Parse()

	if err != nil {
		parse.WriteHelp(os.Stdout)
		os.Exit(64) // system's errno for flag error
	}
	// load config file

	err = config.Load(opts.Config)
	if err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}

	// init log
	err = log.Init()
	if err!=nil {
		fmt.Println(err)
		os.Exit(-1)
	}

	log.Debug("APS:BEGIN")
	svr := NewServer()
	svr.Init()
	if opts.Daemon {
		svr.Daemon()
	}
	svr.Run()
	log.Debug("APS:END")
}






